/******************************************************************** 
Archivo MAIN del proyecto SEM, JUNIO 2011
*********************************************************************/ 

#include "io430x22x4.h"
#include "timer.h" 
#include "intrinsics.h"
#include "comparator.h"
#include "adc.h"
#include "cap.h"
#include "sem_core.h"
#include "sem_logger.h"
#include "stdio.h"

//----------------------- comiezo declaracion de variables -----------------------------
  extern int c_active;
  timer_t main_actual_time;
//----------------------- fin declaracion de variables ---------------------------------

//-------------------------- comienzo funciones auxiliares -----------------------------
void system_init();    // timerA, registrar la ISR
void system_sleep();   // modo LPM3
void comparator_init();

/*********************************************************************
Inicializacion del sistema.
Debe inicializar el timerA, registrar la ISR
*********************************************************************/ 

void system_init(){

/* Internal Very Low Power, Low Frequency Oscillator
The internal very-low-power, low-frequency oscillator (VLO) provides a typical frequency of 12 kHz 
VLOCLK source is selected by setting LFXT1Sx = 10 when XTS = 0. 
The OSCOFF bit disables the VLO for LPM4. 
The LFXT1 crystal oscillators are disabled when the VLO is selected reducing current consumption.
*/
    BCSCTL3 =  LFXT1S_2;
 
    P1DIR |= 0x01;    // P1.0 output, Led rojo conectado a P1.0
    P1DIR |= 0x02;    // P1.1 output, Led verde conectado a P1.1
    TACCTL0 = CCIE;  // modo comparador
    TACCR0 = 375;   // umbral=375 (250ms)
    TACTL = TASSEL_1  + MC_1 + TACLR + ID_3; // up_mode, ACLK, clear, /8
    __enable_interrupt(); // GIE=1
    return;
    }

/***********************************************************************
El sistema pasa a un modo inactivo de bajo consumo (LPM3)
**********************************************************************/ 
void system_sleep(){
  __bis_SR_register(LPM3_bits+GIE);             // Enter LPM3
    return;
    }
//-------------------------- fin funciones auxiliaes -----------------------------------------

main() {
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
 
    //Inicializacion del timer:
    timer_t init_time_value=(timer_t){0,0,0,0};
    timer_init (init_time_value);
    
    //Inicializacion del sistema:
     system_init(); 
     sem_init();
     //system_sleep();
     
     sem_log_init();
     sem_start_partial_metering();
     
     int flag_get_partial = 0;
     int flag_get_partial_done = 0;


    while(1){
      main_actual_time = timer_actual_time(); // copia cuenta de la hora a variable global
      if (main_actual_time.milisecond == 250){
        flag_get_partial = 1;
      }

      if (main_actual_time.milisecond == 750){
        flag_get_partial = 0;
        flag_get_partial_done = 0;
      }

      if ((flag_get_partial != 0) && (flag_get_partial_done == 0)){
        sem_log_partial_q(c_active,main_actual_time.second); // id, sequence
        flag_get_partial_done = 1;
      }

    }
}
